package com.dao;

import com.domain.Account;
import com.tool.Connecttool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class AccountdaoImpl extends dao implements Accountdao {
    @Autowired
    private Connecttool connecttool;
    @Override
    public Account findaccountbyid(Integer id) {
        List<Account>list=super.getJdbcTemplate().query("select " +
                        "* " +
                        "from " +
                        "account" +
                        " " +
                        "where " +
                "id=?",
                new BeanPropertyRowMapper<Account>(Account.class),id);
        if (list.isEmpty())return null;
        return list.get(0);
    }

    @Override
    public Account findaccountbyname(String name) {
        List<Account>list=super.getJdbcTemplate().query("select * " +
                        "from " +
                        "account" +
                        " " +
                        "where " +
                        "name=?",
                new BeanPropertyRowMapper<Account>(Account.class),name);
        if (list.isEmpty())return null;
        if (list.size()>1)throw new RuntimeException("只能为1！");
        return list.get(0);
    }

    @Override
    public List<Account> findall() {
        return getJdbcTemplate().query("select * from account",
                new BeanPropertyRowMapper<>(Account.class));
    }

    @Override
    public void update(Account account) {
        getJdbcTemplate().update("update limanspring.account\n" +
                "set " +
                "  name = ?,\n" +
                "  money = ?\n" +
                "where id = ?;",account.getName(),
                account.getMoney(),account.getId());
    }
}
